package com.rcm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.rcm.dao.CoursesinfoMapper;
import com.rcm.dao.StudentinfoMapper;
import com.rcm.model.Coursesinfo;
import com.rcm.model.Studentinfo;
import com.rcm.utility.MyBatisTool;

/**
 * Servlet implementation class RetrieveServlet
 */
@WebServlet("/RetrieveServlet")
public class RetrieveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentinfoMapper mapper=null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RetrieveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
		String sessionCode = (String) request.getSession().getAttribute("code");
		System.out.println(sessionCode);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		if (sessionCode != null) {
			// 获取页面提交的验证码
			String inputCode = request.getParameter("code");
			System.out.println("页面提交的验证码:" + inputCode);
			if (sessionCode.toLowerCase().equals(inputCode.toLowerCase())) {
				System.out.println("验证成功");
				String sid =request.getParameter("sid");
				String email =request.getParameter("email");
				String password =request.getParameter("password");
				SqlSession session=MyBatisTool.getSession();
				mapper = session.getMapper(StudentinfoMapper.class);
				List<Studentinfo> list=mapper.selectAll();

				//判断学号和邮箱账号是否一致
				for(Studentinfo c:list){
					if(c.getSid().equals(sid)&&c.getEmail().equals(email)) {
						Studentinfo studentinfo = new Studentinfo();
						studentinfo.setPassword(password);
						studentinfo.setSid(sid);
						System.out.println(studentinfo);
						mapper.updatepassword(studentinfo);
						session.close();
						out.print("<script language='javascript'>alert('找回密码成功，请重新登录!');window.location='/ElectiveSystem/index.jsp';</script>");
						return;
					}
				}
				out.print("<script language='javascript'>alert('修改失败，请确定学号和邮箱是否正确!');window.location='retrievepassword.jsp';</script>");
				return;

			} else {
				// 验证失败
				out.print(
						"<script language='javascript'>alert('邮箱验证码错误');window.location.href='retrievepassword.jsp';</script>");
			}
		} else {
			// 验证失败
			out.print(
					"<script language='javascript'>alert('邮箱验证码错误');window.location.href='retrievepassword.jsp';</script>");
		}
	} 
		// 移除session中的验证码
	catch (Exception e) {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<script language='javascript'>alert('修改失败，请确定学号和邮箱是否正确!');window.location='index.jsp';</script>");
		return;
			
		}
		request.removeAttribute("code");
		
	} 
		
	



	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
